package com.example.dellpc.myapplication;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.hardware.Camera;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.SurfaceView;
import android.widget.Button;

import com.example.utils.PermissionUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.tbruyelle.rxpermissions.RxPermissions;

import java.io.IOException;

import rx.functions.Action1;

/**
 * Created by 刘智强 on 2016/9/13.
 * 6.0以后动态权限工具类的测试
 */
public class PermissionActivity extends Activity {
    @ViewInject(R.id.sf_camera)
    private SurfaceView surfaceView;
    @ViewInject(R.id.btn_getpermission)
    private Button btn_getpermiission;
    private Camera camera = null;
    private static final String PACKAGE_URL_SCHEME = "package:"; // 方案

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_permission);
        ViewUtils.inject(this);
        init();
    }

    public void init() {

    }

    @OnClick(R.id.btn_getpermission)
    public void onClick(Button button) {
        switch (button.getId()) {

            case R.id.btn_getpermission:

                PermissionUtils.getInstance().requestPermission(getApplicationContext(), new PermissionUtils.PermissionResultCallBack() {
                    @Override
                    public void onPermissionGranted() {
                        // 当所有权限的申请被用户同意之后,该方法会被调用
                        Utils.toast(getApplicationContext(), "获取相机权限成功!!!");
                        camera = Camera.open(0);
                        try {
                            camera.setPreviewDisplay(surfaceView.getHolder());
                            camera.startPreview();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                    }

                    @Override
                    public void onPermissionDenied(String... permissions) {
                        // 当权限申请中的某一个或多个权限,被用户曾经否定了,并确认了不再提醒时,也就是权限的申请窗口不能再弹出
                        Utils.toast(getApplicationContext(), "获取相机权限失败!!!");

                    }

                    @Override
                    public void onRationalShow(String... permissions) {
                        // 当权限申请中的某一个或多个权限,被用户否定了,但没有确认不再提醒时,也就是权限窗口申请时,但被否定了之后
                        //进入设置页面
                        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                        intent.setData(Uri.parse(PACKAGE_URL_SCHEME + getPackageName()));
                        startActivity(intent);

                    }
                }, Manifest.permission.CAMERA);
                break;

            default:
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        camera.stopPreview();
        camera.release();
        camera = null;
    }
}
